package com.ruoyi.web.controller.teacher;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.BasisPosition;
import com.ruoyi.dto.BasisLoadingPositionCamp;
import com.ruoyi.dto.BasisLoadingPositionWeather;
import com.ruoyi.service.IBasisEquipmentInformationvService;
import com.ruoyi.service.IBasisPositionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 阵地数据管理Controller
 * 
 * @author ruoyi
 * @date 2025-05-23
 */
@RestController
@RequestMapping("/system/position")
public class BasisPositionController extends BaseController
{
    @Autowired
    private IBasisPositionService basisPositionService;

    @Autowired
    private IBasisEquipmentInformationvService basisEquipmentInformationvService;

    /**
     * 查询阵地数据管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BasisPosition basisPosition)
    {
        startPage();
        List<BasisPosition> list = basisPositionService.selectBasisPositionList(basisPosition);
        return getDataTable(list);
    }


    @GetMapping("/findAllPosition")
    public AjaxResult findAllPosition()
    {
        List<BasisPosition> list = basisPositionService.selectBasisPositionList(new BasisPosition());
        return success(list);
    }

    /**
     * 导出阵地数据管理列表
     */
    @Log(title = "阵地数据管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasisPosition basisPosition)
    {
        List<BasisPosition> list = basisPositionService.selectBasisPositionList(basisPosition);
        ExcelUtil<BasisPosition> util = new ExcelUtil<BasisPosition>(BasisPosition.class);
        util.exportExcel(response, list, "阵地数据管理数据");
    }

    /**
     * 获取阵地数据管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(basisPositionService.selectBasisPositionById(id));
    }

    /**
     * 新增阵地数据管理
     */
    @Log(title = "阵地数据管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasisPosition basisPosition)
    {
        UUID uuid = UUID.randomUUID();
        basisPosition.setUuid(uuid.toString());
        basisPosition.setType("阵地");
        return toAjax(basisPositionService.insertBasisPosition(basisPosition));
    }
    /**
     * 修改阵地数据管理
     */
    @Log(title = "阵地数据管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasisPosition basisPosition)
    {
        return toAjax(basisPositionService.updateBasisPosition(basisPosition));
    }

    /**
     * 删除阵地数据管理
     */
    @Log(title = "阵地数据管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(basisPositionService.deleteBasisPositionByIds(ids));
    }

    /**
     * 添加案例与阵地与天气关联表
     */
//    @Log(title = "阵地数据管理", businessType = BusinessType.INSERT)
//    @PostMapping("/configPositionAndWeather")
//    public AjaxResult configPositionAndWeather(@RequestBody BasisLoadingPositionWeather basisPosition)
//    {
//
//        return toAjax(basisPositionService.configPositionAndWeather(basisPosition));
//    }


    /**
     * 添加案例与阵地与阵营
     */

    @Log(title = "阵地数据管理", businessType = BusinessType.INSERT)
    @PostMapping("/addBasisLoadingPositionCamp")
    public AjaxResult addBasisLoadingPositionCamp(@RequestBody BasisLoadingPositionCamp basisLoadingPositionCamp)
    {
        return toAjax(basisPositionService.addBasisLoadingPositionCamp(basisLoadingPositionCamp));
    }

    @Log(title = "阵地数据管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBasisLoadingPositionCamp")
    public AjaxResult updateBasisLoadingPositionCamp(@RequestBody BasisLoadingPositionCamp basisLoadingPositionCamp)
    {
        return toAjax(basisPositionService.updateBasisLoadingPositionCamp(basisLoadingPositionCamp));
    }

    @GetMapping("/findByBasisLoadingPositionCamp")
    public AjaxResult findByBasisLoadingPositionCamp(String loadingId,Integer positionId)
    {
        return success(basisPositionService.findByBasisLoadingPositionCamp(loadingId, positionId));
    }


    @GetMapping("/delBasisLoadingPositionCamp")
    public AjaxResult delBasisLoadingPositionCamp(String loadingId,Integer positionId)
    {
        boolean flag = false;
        flag = basisPositionService.delBasisLoadingPositionCamp(loadingId, positionId);
        if (flag){
            flag =  basisEquipmentInformationvService.delBasisPositionEquipmentInformationv(loadingId, positionId);
            if (!flag) return success(flag);
            flag = basisPositionService.delBasisPositionEquipmentUser(loadingId, positionId);
            if (!flag) return success(flag);
            flag = basisPositionService.delBasisPositionUser(loadingId, positionId);
            if (!flag) return success(flag);
        }


        return success(flag);
    }




    //根据案例id和阵营id查询阵地
    @GetMapping("/findByLoadingIdAndCampIdFormPosition")
    public AjaxResult findByLoadingIdAndCampIdFormPosition(String loadingId,String campId)
    {
        return success(basisPositionService.findByLoadingIdAndCampId(loadingId, campId));
    }

    //根据案例id查询 没有跟当前案例相关联的阵地
    @GetMapping("/selectBasisPositionFormLoadingId")
    public AjaxResult selectBasisPositionFormLoadingId(String loadingId) {
        return success(basisPositionService.selectBasisPositionFormLoadingId(loadingId));
    }

    //根据案例id查询 没有跟当前案例相关联的阵地
    @GetMapping("/getByUuid")
    public AjaxResult getByUuid(String uuid) {
        BasisPosition byUuid = basisPositionService.getByUuid(uuid);
        return success(byUuid);
    }

}
